#pragma once
namespace MqClient
{

inline bool check_amqp_error(amqp_rpc_reply_t x, char const *context) {
    switch (x.reply_type) {
    case AMQP_RESPONSE_NORMAL:
        return true;

    case AMQP_RESPONSE_NONE:
        Log::error("%s: missing RPC reply type!", context);
        break;

    case AMQP_RESPONSE_LIBRARY_EXCEPTION:
        Log::error("%s: %s", context, amqp_error_string2(x.library_error));
        break;

    case AMQP_RESPONSE_SERVER_EXCEPTION:
        switch (x.reply.id) {
        case AMQP_CONNECTION_CLOSE_METHOD: 
            {
                amqp_connection_close_t *m =
                    (amqp_connection_close_t *)x.reply.decoded;
                Log::error("%s: server connection error %uh, message: %.*s",
                    context, m->reply_code, (int)m->reply_text.len,
                    (char *)m->reply_text.bytes);
                break;                         
            }
        case AMQP_CHANNEL_CLOSE_METHOD: 
            {
                amqp_channel_close_t *m = (amqp_channel_close_t *)x.reply.decoded;
                Log::error("%s: server channel error %uh, message: %.*s",
                    context, m->reply_code, (int)m->reply_text.len,
                    (char *)m->reply_text.bytes);
                break;
            }
        default:
            Log::error("%s: unknown server error, method id 0x%08X",
                context, x.reply.id);
            break;
        }
        break;
    }

    return false;
}
}